package 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	
	public class CustomPreloader extends DownloadProgressBar 
	{ 
  
	    [Embed(source="preloader.swf", symbol="Preloader")]  
	    public var WelcomeScreenGraphic:Class;
	    
	    public var wcs:MovieClip; 
  
		public function CustomPreloader():void
		{ 
			//var tt:String = Application.application.parameters.versionType;		
			super(); 
            wcs = new WelcomeScreenGraphic();
            addChild(wcs); 
            wcs.gotoAndStop(0);
		}
		 
        public override function set preloader(preloader:Sprite):void 
        {                   
            preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);    
            preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete);
            preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
            preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
            
            centerPreloader();
        }	
        private function centerPreloader():void
        {
            //x = (stageWidth / 2) - (wcs.width / 2);
            //y = (stageHeight / 2) - (wcs.height / 2);
            
            x = 493 - (wcs.width / 2) + 140;
            y = 499 - (wcs.height / 2)-10 ;
                        
        }
        private function onSWFDownloadProgress(event:ProgressEvent):void
        {
            var t:Number = event.bytesTotal;
            var l:Number = event.bytesLoaded;
            var p:Number = Math.round((l/t) * 20);
            trace("gotoAndStop(p) : " + p); 
  
            var pForAmount:int = Math.floor(p * 5);
            wcs.gotoAndStop(p);
            wcs.amount_txt.text = String(pForAmount) + "%";

        }
        private function onSWFDownloadComplete(event:Event):void
        {
            wcs.gotoAndStop(100); 
            wcs.amount_txt.text = "100%";
        }
        private function onFlexInitProgress(event:FlexEvent):void
        {
            wcs.gotoAndStop(100);
            wcs.amount_txt.text = " ";
        }
        private function onFlexInitComplete(event:FlexEvent):void 
        {
            wcs.gotoAndStop(100);
            dispatchEvent( new Event(Event.COMPLETE));
        }
	}
}